﻿using System;
using System.Collections.Generic;
using System.Configuration;

namespace Hxsoft.AppV1
{
	public class DictionaryRule
	{
		/// <summary>
		/// 取得指定项目字典类别的掩码配置信息。
		/// </summary>
		/// <param name="className">字典类别名称。</param>
		/// <returns>指定项目字典类别的掩码配置信息。</returns>
		private static DictionaryElement GetMaskConfiguration(string className)
		{
			DictionaryRuleSection dictionaryRule = ApplicationConfiguration.Configuration.DictionaryRule;
			return dictionaryRule.Dictionarys.GetItem(className);
		}




		/// <summary>
		/// 取得指定项目字典值所对应的掩码值。
		/// </summary>
		/// <param name="className">字典的类别。</param>
		/// <param name="value">字典值。</param>
		/// <returns>指定项目字典值所对应的掩码值。</returns>
		public static long GetMaskCode(string className, long value)
		{
			DictionaryElement dictionary = GetMaskConfiguration(className);
			return dictionary.GetMaskCode(value);
		}




		/// <summary>
		/// 取得指定项目字典值的掩码集合。
		/// </summary>
		/// <param name="className">字典的类别。</param>
		/// <returns>指定项目字典值的掩码集合。</returns>
		public static long[] GetMaskCodes(string className)
		{
			DictionaryElement dictionary = GetMaskConfiguration(className);
			return dictionary.GetMaskCodes();
		}




		/// <summary>
		/// 取得指定项目字典指定占位项的掩码值。
		/// </summary>
		/// <param name="className">字典的类别。</param>
		/// <param name="name">指定的占位项名称。</param>
		/// <returns>指定项目字典指定占位项的掩码值。</returns>
		public static long GetMaskCode(string className, string name)
		{
			DictionaryElement dictionary = GetMaskConfiguration(className);
			return dictionary.GetMaskCode(name);
		}




		/// <summary>
		/// 取得指定值的指定项值。
		/// </summary>
		/// <param name="className">字典的类别。</param>
		/// <param name="value">指定值。</param>
		/// <param name="pointer">指定项位置。</param>
		/// <returns>指定值的指定项值。</returns>
		public static long? GetValue(string className, long value, int pointer)
		{
			long[] values = GetValues(className, value);
			if(values.Length > pointer)
				return values[pointer];
			return null;
		}

		/// <summary>
		/// 取得指定值的指定项值。
		/// </summary>
		/// <param name="className">字典的类别。</param>
		/// <param name="value">指定值。</param>
		/// <param name="itemName">指定项名称。</param>
		/// <returns>指定值的指定项值。</returns>
		public static long? GetValue(string className, long value, string itemName)
		{
			DictionaryRuleSection dictionaryRule = ApplicationConfiguration.Configuration.DictionaryRule;
			int itemIndex = dictionaryRule.Dictionarys.GetItem(className).Occupys.GetIndex(itemName);
			return GetValue(className, value, itemIndex);
		}




		/// <summary>
		/// 取得指定值的各项值。
		/// </summary>
		/// <param name="className">字典的类别。</param>
		/// <param name="value">指定值。</param>
		/// <returns>指定值的各项值。</returns>
		public static long[] GetValues(string className, long value)
		{
			long[] maskCodes = DictionaryRule.GetMaskCodes(className);
			List<long> values = new List<long>();
			for (int i = 0; i < maskCodes.Length; i++)
			{
				long itemValue = value / maskCodes[i] * maskCodes[i];
				if (values.Count > 0 && values[values.Count - 1] == itemValue)
					break;
				values.Add(itemValue);
			}
			return values.ToArray();
		}
	}
}
